package com.ruoyi.demowechatpay.config;


import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.EcommerceService;
import com.github.binarywang.wxpay.service.MerchantTransferService;
import com.github.binarywang.wxpay.service.TransferService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.EcommerceServiceImpl;
import com.github.binarywang.wxpay.service.impl.MerchantTransferServiceImpl;
import com.github.binarywang.wxpay.service.impl.TransferServiceImpl;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.ruoyi.demowechatpay.service.MyMerchantTransferServiceImpl;
import com.ruoyi.demowechatpay.service.WxPayServiceV3;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Binary Wang
 */
@Configuration
@ConditionalOnClass({WxPayService.class, EcommerceService.class, MerchantTransferService.class, MyMerchantTransferServiceImpl.class, WxPayServiceV3.class})
@EnableConfigurationProperties(WxPayProperties.class)
@AllArgsConstructor
public class WxPayConfiguration {
    private WxPayProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
        payConfig.setApiV3Key(StringUtils.trimToNull(this.properties.getApiV3Key()));
        payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
        payConfig.setPrivateKeyPath(StringUtils.trimToNull(this.properties.getPrivateKeyPath()));
        payConfig.setPrivateCertPath(StringUtils.trimToNull(this.properties.getPrivateCertPath()));
        payConfig.setPublicKeyPath(StringUtils.trimToNull(this.properties.getPublicKeyPath()));
        payConfig.setPublicKeyId(StringUtils.trimToNull(this.properties.getPublicKeyId()));
        payConfig.setNotifyUrl(StringUtils.trimToNull(this.properties.getNotifyUrl()));
        // 可以指定是否使用沙箱环境 微信支付V3 目前不支持沙箱模式
        payConfig.setUseSandboxEnv(false);

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    @Bean
    @ConditionalOnMissingBean
    public EcommerceService ecService() {
        EcommerceService ecommerceService = new EcommerceServiceImpl(this.wxService());
        return ecommerceService;
    }

    @Bean
    @ConditionalOnMissingBean
    public MerchantTransferService transferService() {
        MerchantTransferService transferService = new MerchantTransferServiceImpl(this.wxService());
        return transferService;
    }

    @Bean
    @ConditionalOnMissingBean
    public TransferService transferService1() {
        TransferService transferService = new TransferServiceImpl(this.wxService());
        return transferService;
    }

    @Bean
    @ConditionalOnMissingBean
    public MyMerchantTransferServiceImpl myTransferService() {
        MyMerchantTransferServiceImpl transferService = new MyMerchantTransferServiceImpl(this.wxService());
        return transferService;
    }

}
